package com.aat.dxfy.redis.pool;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;

public class RedisUtil {

	private static CustomPool getPool() {
		return RedisSentinelPoolHolder.getInstance();
	}

	private static void returnResource(CustomPool pool, Jedis jedis) {
		if (jedis != null) {
			pool.returnResource(jedis);
		}
	}

	public static boolean keyExist(String key){
		boolean value = false;
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.exists(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	public static boolean keyExist(byte[] key){
		boolean value = false;
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.exists(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	public static long getListLength(String key){
		long value = 0;
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.llen(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	public static String get(String key){
		String value = null;
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.get(key);
		} catch (JedisConnectionException e) {
			try {
				value = jedis.get(key);
			} catch (Exception e1) {
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	public static Object getObject(String key, Class<?> clazz) {
		String JsonString = get(key);
		if(JsonString==null){
			return null;
		}
			
		return JSONObject.parseObject(JsonString, clazz);
	}

	public static void set(String key, Object value) {
		String JsonString = value==null ? null : JSONObject.toJSONString(value);
		set(key, JsonString);
	}

	public static void setex(String key, Object value, int seconds) {
		String JsonString = value==null ? null : JSONObject.toJSONString(value);
		setex(key, JsonString, seconds);
	}

	public static byte[] get(byte[] key){
		byte[] value = null;
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.get(key);
		} catch (JedisConnectionException e) {
			try {
				value = jedis.get(key);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	public static void set(String key, String value){
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.set(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(pool, jedis);
		}
	}

	public static void setex(String key, String value, int seconds){
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.setex(key, seconds, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(pool, jedis);
		}
	}

	public static void expire(String key, int seconds){
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.expire(key, seconds);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(pool, jedis);
		}
	}

	public static void expire(byte[] key, int seconds){
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.expire(key, seconds);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(pool, jedis);
		}
	}

	public static void setex(byte[] key, byte[] value, int seconds) {
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.setex(key, seconds, value);
		} catch (Exception e) {
		} finally {
			returnResource(pool, jedis);
		}
	}

	public static void set(byte[] key, byte[] value) {
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.set(key, value);
		} catch (Exception e) {
		} finally {
			returnResource(pool, jedis);
		}
	}

	public static void del(String key) {
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.del(key);
		} catch (Exception e) {
		} finally {
			returnResource(pool, jedis);
		}
	}

	public static void del(byte[] key) {
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.del(key);
		} catch (Exception e) {
		} finally {
			returnResource(pool, jedis);
		}
	}

	public static void push(String key, String value) {
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.lpush(key, value);
		} catch (Exception e) {
		} finally {
			returnResource(pool, jedis);
		}
	}

	public static void push(String key, List<String> values) {
		CustomPool pool = null;
		Jedis jedis = null;
		int length = values.size();
		String[] vs = new String[length];
		for (int i = 0; i < length; i++) {
			vs[i] = values.get(i);
		}

		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.lpush(key, vs);
		} catch (Exception e) {
		} finally {
			returnResource(pool, jedis);
		}
	}

	public static String pop(String key) {
		String value = null;
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.rpop(key);
		} catch (Exception e) {
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	public static List<String> pops(String key, int count) {
		List<String> value = new ArrayList<String>();
		CustomPool pool = null;
		Jedis jedis = null;

		try {
			pool = getPool();
			jedis = pool.getResource();
			for (int i = 0; i < count; i++) {
				String result = jedis.rpop(key);
				if (result != null) {
					value.add(result);
				} else {
					break;
				}

			}
		} catch (Exception e) {
		} finally {
			returnResource(pool, jedis);
		}
		return value;
	}

	public static void publish(String channel, String message) {
		CustomPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.publish(channel, message);
		} catch (Exception e) {
			System.out.println(e);
		} finally {
			returnResource(pool, jedis);
		}
	}
}
